package cn.bigcontainer.alt.leditor.service.impl;

import cn.bigcontainer.alt.leditor.service.LeditorService;
import cn.bigcontainer.alt.meta.dto.MCrud;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import cn.bigcontainer.alt.meta.service.MAppService;
import cn.bigcontainer.alt.meta.service.MCrudService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
@Slf4j
public class LeditorServiceImpl implements LeditorService {

    @Override
    public MCrud loadLeditor(String id, Map<String, Object> req) {
        return crudService.loadCrud(id, req);
    }

    @Resource
    private MAppService woAppService;

    @Override
    public MViewDto loadListData(String id, Map<String, Object> req) {
        MCrud leditor = crudService.loadCrud(id, req);
        leditor.getCreateView().getProperties().forEach(vp -> {
            leditor.getObject().getRequest().remove(vp.getId());
        });
        leditor.getUpdateView().getProperties().forEach(vp -> {
            leditor.getObject().getRequest().remove(vp.getId());
        });
        // 加载列表数据
        MViewDto list = leditor.getUpdateView();
        MViewDto search = leditor.getSearchView();
        MViewDto listNew = leditor.beforeLoadUpdateForm(list, null);
        if (listNew != null) {
            return listNew;
        }
        woAppService.loadListData(list, search);
        leditor.afterLoadUpdateForm(list, null);
        // 加载创建表单数据
        MViewDto form = leditor.getCreateView();
        MViewDto fnew = leditor.beforeLoadCreateForm(form, null);
        if (fnew != null) {
            return fnew;
        }
        woAppService.loadCreateForm(form, null);
        leditor.afterLoadCreateForm(form, null);
        return list;
    }

    @Override
    public MViewDto submitCreateForm(String id, Map<String, Object> req) {
        return crudService.submitCreateForm(id, req);
    }

    @Resource
    private MCrudService crudService;

    @Override
    public MViewDto submitUpdateForm(String id, Map<String, Object> req) {
        return crudService.submitUpdateForm(id, req);
    }
}
